package com.data.boot.service;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.den_4.inotify_java.EventQueueFull;
import com.den_4.inotify_java.InotifyEvent;
import com.den_4.inotify_java.InotifyEventListener;
import com.den_4.inotify_java.enums.Event;

/**
 * Created by dwk on 2019/6/4.
 */
public class MyInotifyEventListener implements InotifyEventListener {

    private static final Log log = LogFactory.get();

    @Override
    public void filesystemEventOccurred(InotifyEvent inotifyEvent) {
        Event event = null;
        try {
            event = Event.fromValue(inotifyEvent.getMask());
            log.info("inotify event, event: {}, name: {}", event.name(), inotifyEvent.getContextualName());
            LoadDataServiceFactory factory = LoadDataServiceFactory.getLoadDataServiceFactory();
            switch (event) {
                case Close_Write://写入完毕
                    //数据装载
//                    factory.newInstance(inotifyEvent.getContextualName()).load();
                    break;
                case Create://创建
                    break;
                case Moved://移动
                    break;
                case Moved_From://原文件
                    break;
                case Moved_To://新文件
                    break;
                case Delete://删除
                    break;
                case Create_Dir:
                    break;
                case Delete_Dir:
                    break;
                case Delete_Self:
                    log.info("监听目录{}被删除,监听停止", inotifyEvent.getContextualName());
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            log.error("mask:{},name:{},event:{}", inotifyEvent.getMask(), inotifyEvent.getContextualName(), event.name());
        }
    }

    @Override
    public void queueFull(EventQueueFull eventQueueFull) {

    }

}
